1417C - k-Amazing Numbers - CodeForces Solution


data structures *1500

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const ll mod = 1e9 + 7;
const ll inf = 1e18;
#define speedup ios_base::sync_with_stdio(false);cin.tie(NULL);
ll gcd(ll a, ll b) {
	if (b == 0) {
		return a;
	}
	return gcd(b, a % b);
}
namespace modop {
	ll madd(ll a, ll b) {
		return (a + b) % mod;
	}
	ll msub(ll a, ll b) {
		return (((a - b) % mod) + mod) % mod;
	}
	ll mmul(ll a, ll b) {
		return ((a % mod) * (b % mod)) % mod;
	}
	ll mpow(ll base, ll exp) {
		ll res = 1;
		while (exp) {
			if (exp % 2 == 1) {
				res = (res * base) % mod;
			}
			exp >>= 1;
			base = (base * base) % mod;
		}
		return res;
	}
	ll minv(ll base) {
		return mpow(base, mod - 2);
	}
	ll mdiv(ll a, ll b) {
		return mmul(a, minv(b));
	}
	const ll FACTORIAL_SIZE = 1.1e6;
	ll fact[FACTORIAL_SIZE], ifact[FACTORIAL_SIZE];
	void gen_factorial(ll n) {
		fact[0] = fact[1] = ifact[0] = ifact[1] = 1;

		for (ll i = 2; i <= n; i++) {
			fact[i] = (i * fact[i - 1]) % mod;
		}
		ifact[n] = minv(fact[n]);
		for (ll i = n - 1; i >= 2; i--) {
			ifact[i] = ((i + 1) * ifact[i + 1]) % mod;
		}
	}
	ll nck(ll n, ll k) {
		if (k == 0) {
			return 1;
		}
		ll den = (ifact[k] * ifact[n - k]) % mod;
		return (den * fact[n]) % mod;
	}
}
using namespace modop;
ll sd(ll a) {
	ll res = 0;
	while (a) {
		ll dig = a % 10;
		res += dig;
		a /= 10;
	}
	return res;
}
void solve() {
	int n;
	cin >> n;
	map<int, vector<int>> pos;
	vector<int> v(n + 1);
	for (int i = 1; i <= n; i++) {
		cin >> v[i];
		pos[v[i]].push_back(i);
	}
	vector<int> ans(n + 1, -1);
	vector<bool> vis(n + 1);
	map<int, int> first;
	for (auto x : pos) {
		int diff = -1;
		int s = x.second.size();
		for (int i = 1; i < s; i++) {
			diff = max(diff, x.second[i] - x.second[i - 1]);
		}
		diff = max(diff, n - x.second.back() + 1);
		diff = max(diff, x.second.front());
		first[x.first] = diff;
	}
	/*for (auto x : first) {
		cout << x.first << " " << x.second << endl;
	}*/
	for (auto x : first) {
		int now = x.second;
		while (now <= n and !vis[now]) {
			vis[now] = 1;
			ans[now] = x.first;
			now++;
		}
	}
	for (int i = 1; i <= n; i++) {
		cout << ans[i] << " ";
	}
	cout << endl;
}
int main() {
	speedup;
	int t;
	cin >> t;
	while (t--) {
		solve();
	}
}


Comments

Submit
0 Comments
More Questions

1593C - Save More Mice
1217. Minimum Cost to Move Chips to The Same Position
347. Top K Frequent Elements
1503. Last Moment Before All Ants Fall Out of a Plank
430. Flatten a Multilevel Doubly Linked List
1290. Convert Binary Number in a Linked List to Integer
1525. Number of Good Ways to Split a String
72. Edit Distance
563. Binary Tree Tilt
1306. Jump Game III
236. Lowest Common Ancestor of a Binary Tree
790. Domino and Tromino Tiling
878. Nth Magical Number
2099. Find Subsequence of Length K With the Largest Sum
1608A - Find Array
416. Partition Equal Subset Sum
1446. Consecutive Characters
1618A - Polycarp and Sums of Subsequences
1618B - Missing Bigram
938. Range Sum of BST
147. Insertion Sort List
310. Minimum Height Trees
2110. Number of Smooth Descent Periods of a Stock
2109. Adding Spaces to a String
2108. Find First Palindromic String in the Array
394. Decode String
902. Numbers At Most N Given Digit Set
221. Maximal Square
1200. Minimum Absolute Difference
1619B - Squares and Cubes